package work.managercenter;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import utils.MyUtils;

public class UpdateSolr {
	public static void main(String[] args) {
		try {
			BufferedReader br = new BufferedReader(new FileReader("/home/gmarcone/workspace_ib_mc/Stuff/lib/complex_id.txt"));
			String line;
			int i = 0;
			System.out.println(System.currentTimeMillis());
			while ((line = br.readLine()) != null) {
				final String complex = line.trim();
				Thread t = new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							update(complex);
						} catch (Exception e) {
							e.printStackTrace(System.out);
						}
					}
				});
				if ((++i) % 100 == 0) {
					System.out.println("Started " + i);
					System.out.println(System.currentTimeMillis());
					System.out.println();
				}
				t.start();
				Thread.sleep(100);
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}

	private static void update(String complexId) throws Exception {
		String urlAddress =  "http://stg-aptrest1.internetbrands.com:8080/services/superadmin/v1/updateAptRatingsSolrDoc";

		String userGuid = "83a861fa-20f3-48ae-ac24-f42a822254eb";

		String params = "userGuid=" + java.net.URLEncoder.encode(userGuid,"UTF-8") +
				"&complexId=" + java.net.URLEncoder.encode(complexId,"UTF-8") +
				"&format=json";

		URL url = new URL(urlAddress + "?" + params);
		HttpURLConnection conn1 = (HttpURLConnection) url.openConnection();
		conn1.setRequestMethod("GET");
		conn1.setRequestProperty("Accept", "application/json");
		conn1.setDoOutput(true);

		BufferedReader br = new BufferedReader(new InputStreamReader((conn1.getInputStream())));

		String output;
		StringBuilder json = new StringBuilder();

		while ((output = br.readLine()) != null) {
			json.append(output).append(MyUtils.EOL);
		}

		conn1.disconnect();

		//System.out.println("json: " + json);
	}
}
